//
//  Encryption.swift
//  LiveTrivia
//
//  Created by 李响 on 2017/12/11.
//  Copyright © 2017年 LiveTrivia. All rights reserved.
//

import Foundation
import CommonCrypto

extension String {
    
    var md5: String {
        guard let data = data(using: .utf8) else {
            return self
        }
        
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        
        _ = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
            return CC_MD5(bytes.baseAddress, CC_LONG(data.count), &digest)
        }
        
        return digest.map { String(format: "%02x", $0) }.joined()
    }
}
